<!doctype html>
<html lang="en">
	<head>
		<title>Tween.js / dynamic to object</title>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<style>
			body {
				margin: 0px;
			}

			#scene1,
			#scene2 {
				display: inline-block;
				font-size: 13px;
				padding-right: 32px;
			}
		</style>
		<link href="css/style.css" media="screen" rel="stylesheet" type="text/css" />
	</head>
	<body>
		<div id="info" style="position: relative">
			<h1><a href="http://github.com/tweenjs/tween.js">tween.js</a></h1>
			<h2>07 _ dynamic to object</h2>
			<p>tweening towards a moving target</p>

			<div id="scene1">
				<p><code>.dynamic(false)</code></p>
			</div>
			<div id="scene2">
				<p><code>.dynamic(true)</code></p>
			</div>
		</div>

		<script type="module">
			import {Tween, Group, Easing} from '../dist/tween.esm.js'
			import {drawRabbit, drawFox} from './js/drawings.js'

			startScene('scene1', false)
			startScene('scene2', true)

			function startScene(id, dynamic) {
				const group = new Group()

				const width = 480
				const height = 320

				const scene = document.getElementById(id)

				const canvas = document.createElement('canvas')
				canvas.width = width
				canvas.height = height
				scene.appendChild(canvas)

				const context = canvas.getContext('2d')

				const rabbit = {x: width - 50, y: 50}

				new Tween(rabbit, group)
					.to({x: width - 50, y: height - 50}, 3000)
					.easing(Easing.Exponential.InOut)
					.start()

				const fox = {x: 50, y: 50}

				new Tween(fox, group).to(rabbit, 3000).dynamic(dynamic).duration(3000).easing(Easing.Exponential.InOut).start()

				animate()

				function animate(time) {
					group.update(time)

					// draw background
					context.fillStyle = 'rgb(240,250,240)'
					context.fillRect(0, 0, width, height)

					drawRabbit(context, rabbit.x, rabbit.y, 'rgb(150,150,150)')
					drawFox(context, fox.x, fox.y, 'rgb(200,80,80)')

					requestAnimationFrame(animate)
				}
			}
		</script>
	</body>
</html>
